﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;
 
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
using ZF.WS.Admin;
using ZF.Model;


namespace ZF.BLL.Admin
{

    public partial class MerchandiseList : Merchandise
    {
        [PageArgument(Key = "type", DefaultValue = 0)]
        public int Type { get; set; }
        [PageArgument(Key = "mt", DefaultValue = -1)]
        public int MT { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {

        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.SP_Merchandise, bool>>> filterList = new List<Expression<Func<Model.SP_Merchandise, bool>>>();
                IQueryable<Model.SP_Merchandise> selector = dc.SP_Merchandise;
                selector = selector.Where(p => !p.IsDel);
                switch (this.Type)
                {
                    case 0:
                        break;
                    case 1:
                        filterList.Add(p => p.IsRecommend); break;
                    case 2:
                        filterList.Add(p => p.IsHot); break;
                    case 3:
                        filterList.Add(p => p.Discount <10); break;
                    case 4:
                        filterList.Add(p => p.IsSell); break;
                    case 5:
                        filterList.Add(p => !p.IsSell); break;
                    case 6:
                        filterList.Add(p => p.EndTime.HasValue && p.EndTime.Value < DateTime.Now); break;
                }
                if (MT > 0)
                    filterList.Add(p => p.MerchandiseTypeId == MT);
                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }


                var selWhere = selector.Select(p =>
                    new
                    {
                        Merc = p,
                        Type = p.Dic_MerchandiseType,
                        FreightTemplate= p.SP_Freight,
                    });
                var selPage = selWhere.Where(this.gvList).OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList().Select(p => new
                {
                    Merc = p.Merc,
                    Photo = string.IsNullOrEmpty(p.Merc.Photo) ? "" : p.Merc.Photo.Split(';')[0],
                    Type = p.Type,
                    FreightSetList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FreightSet>>(p.Merc.FreightSet),
                    FTName = p.FreightTemplate == null ? "" : p.FreightTemplate.Name,
                }).ToList();
                this.gvList.DataSource = source;
                this.gvList.DataBind();

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.SP_Merchandise.DeleteAllOnSubmit(
                       dc.SP_Merchandise.Where(p => idArr.Contains(p.MerchandiseId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = CTitle;
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }
        public void Search(object sender, EventArgs e)
        {
            //this.gvList.RemoveAllFilterExpression();
            //if (this.tbTitle.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.Title.Contains(@0)", this.tbTitle.Text);
            //if (this.tbHouseNo.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.HouseId=@0", this.tbHouseNo.Text.Convert<int>()-10000);
            //if (this.ddlHouseType.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.HouseType=@0", this.ddlHouseType.SelectedValue);
            //if (this.ddlHouseStatus.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.Status=@0", this.ddlHouseStatus.SelectedValue.Convert<int>());
            //if (this.tbEndTime1.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            //if (this.tbEndTime2.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());

            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }



        public void Oper(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            var idArrTemp = this.gvList.GetCheckKeys().Select(p => p.Convert<int>()).ToArray();
            if (idArrTemp.Length == 0)
                throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var list = dc.SP_Merchandise.Where(p => idArrTemp.Contains(p.MerchandiseId)).ToList();
                switch (bc.CommandArgument)
                {
                    case "SellUp":
                        list.ForEach(p =>
                        {
                            p.IsSell = true;
                            if (p.EndTime < DateTime.Now)
                                p.EndTime = DateTime.Now.AddMonths(1);

                        });
                        break;
                    case "SellDown":
                        list.ForEach(p =>
                        {
                            p.IsSell = false;
                        });
                        break;
                    case "CancelDiscount":
                        list.ForEach(p =>
                        {
                            p.Discount = 0;
                        });
                        break;
                    case "Del":
                        list.ForEach(p =>
                        {
                            p.IsDel = true;
                        });
                        break;
                    case "IsRecommend":
                        list.ForEach(p =>
                        {
                            p.IsRecommend = !p.IsRecommend;
                        });
                        break;
                }
                bool isSuccess = true;
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "操作";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();
            }
        }
    }
}


